package com.liuxinlong.jobs;

import com.liuxinlong.api.WeChatMessageInterface;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.constant.TimeFormatConstants;
import com.liuxinlong.enums.MatterSourceEnum;
import com.liuxinlong.enums.MessageContentTypeEnum;
import com.liuxinlong.enums.MessagePushBusinessEnum;
import com.liuxinlong.modules.dao.ParameterDao;
import com.liuxinlong.modules.dao.PatentProductClassDao;
import com.liuxinlong.modules.dao.PatentTrendsDao;
import com.liuxinlong.modules.dao.UserMatterDao;
import com.liuxinlong.modules.entity.MessageConfiguration;
import com.liuxinlong.modules.entity.MessagePushRecord;
import com.liuxinlong.modules.entity.PatentProductClass;
import com.liuxinlong.modules.entity.UserMatter;
import com.liuxinlong.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 验证专利动态维护定时任务
 */
@Service
@Slf4j
@Component("checkPatentTrendsTask")
public class CheckPatentTrendsTask {

    @Autowired
    private PatentTrendsDao patentTrendsDao;

    @Autowired
    private UserMatterDao userMatterDao;

    @Autowired
    private PatentProductClassDao patentProductClassDao;

    @Autowired
    private ParameterDao parameterDao;

    @Autowired
    private WeChatMessageInterface weChatMessageInterface;

    /**
     * 业务逻辑
     */
    public void reptilian() {
        log.info("checkPatentTrendsTask start...");
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS);
        String currentDate = currentTime.substring(0,10);
        String monthFirstWednesday = TimeUtils.firstWednesday();
        if (TimeUtils.compareDate(currentDate, monthFirstWednesday, TimeFormatConstants.YYYY_MM_DD) < 0) {
            log.info("Reminder time not reached.");
            log.info("checkPatentTrendsTask end...");
            return;
        }
        String lastTwoMonth = TimeUtils.getAssignLastNMonthThisDay(2, currentTime, TimeFormatConstants.YYYY_MM_DD).substring(0, 7);
        Map<String, Map<String, String>> monthTrendsMap = patentTrendsDao.queryExistPatentTrends(lastTwoMonth);
        List<PatentProductClass> productClassList = patentProductClassDao.getProductClassList();
        List<UserMatter> matterList = new ArrayList<>();
        String content = lastTwoMonth + " 专利动态信息尚未更新,请及时处理！";
        List<String> targetList  = new ArrayList();
        for (PatentProductClass productClass : productClassList) {
            if (StringUtils.isEmpty(productClass.getPatentEngineer())) {
                continue;
            }
            if (monthTrendsMap.containsKey(productClass.getId())) {
                continue;
            }
            UserMatter userMatter = new UserMatter();
            userMatter.setId(SnowFlake.nextIdStr());
            userMatter.setPendingUser(productClass.getPatentEngineer());
            userMatter.setCreateTime(TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS));
            userMatter.setSourceType(MatterSourceEnum.PATENT_TRENDS.getValue());
            userMatter.setContent(content);
            matterList.add(userMatter);
            targetList.add(productClass.getPatentEngineer());
        }
        if (matterList.size() == 0) {
            log.info("All patent trends information has been updated.");
            log.info("checkPatentTrendsTask end...");
            return;
        }
        userMatterDao.insertBatch(matterList);
        MessageConfiguration messageConfiguration = new MessageConfiguration();
        messageConfiguration.setBusinessType(MessagePushBusinessEnum.PATENT_TRENDS.getValue());
        messageConfiguration.setContent(content);
        messageConfiguration.setContentType(MessageContentTypeEnum.TEXT_CARD.getValue());
        MessagePushRecord mpr = weChatMessageInterface.pushMessage(targetList, null, messageConfiguration, currentTime);
        log.info("checkPatentTrendsTask end...");
    }
}

